import { logger } from "../logger";
import * as rcVersion from "../remoteconfig/versionslist";
import { Command } from "../command";
import { needProjectId } from "../projectUtils";
import { requireAuth } from "../requireAuth";
import { requirePermissions } from "../requirePermissions";
import { Version, ListVersionsResult } from "../remoteconfig/interfaces";
import { datetimeString } from "../utils";
import * as Table from "cli-table3";

const tableHead = ["Update User", "Version Number", "Update Time"];

function pushTableContents(table: Table.Table, version: Version): number {
  return table.push([
    version.updateUser?.email,
    version.versionNumber,
    version.updateTime ? datetimeString(new Date(version.updateTime)) : "",
  ]);
}

export const command = new Command("remoteconfig:versions:list")
  .description(
    "get a list of Remote Config template versions that have been published for a Firebase project",
  )
  .option(
    "--limit <maxResults>",
    "limit the number of versions being returned. Pass '0' to fetch all versions",
  )
  .before(requireAuth)
  .before(requirePermissions, ["cloudconfig.configs.get"])
  .action(async (options) => {
    const versionsList: ListVersionsResult = await rcVersion.getVersions(
      needProjectId(options),
      options.limit,
    );
    printVersionsTable(versionsList);

    return versionsList;
  });

export function printVersionsTable(versionsList: ListVersionsResult): void {
  const table = new Table({ head: tableHead, style: { head: ["green"] } });
  for (const version of versionsList.versions || []) {
    pushTableContents(table, version);
  }
  logger.info(table.toString());
}
